package com.zzptc.zzsgz.bdsjws.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.github.lzyzsd.randomcolor.RandomColor;
import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.Add_Contacts_Activity;
import com.zzptc.zzsgz.bdsjws.views.CircleTextImageView;
import com.zzptc.zzsgz.bdsjws.vo.Contacts;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/5/24 024 .
 */

public class Add_Contacts_Adapter extends RecyclerView.Adapter<Add_Contacts_Adapter.ViewHolder>{

    private List<Contacts> list;
    private View itemView;
    private HashMap<String,Contacts> hashMap=MyApplication.checked_contacts_hashMap;

    public Add_Contacts_Adapter(List<Contacts> list){
        this.list = list;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView name,phonenum,address;
        private CircleTextImageView namep;
        private CheckBox checkBox;

        public ViewHolder(View itemView) {
            super(itemView);
            name= (TextView) itemView.findViewById(R.id.name);
            address= (TextView) itemView.findViewById(R.id.address);
            phonenum= (TextView) itemView.findViewById(R.id.phonenum);
            namep= (CircleTextImageView) itemView.findViewById(R.id.namep);
            checkBox= (CheckBox) itemView.findViewById(R.id.checkbox_all_contacts);
            getAdapterPosition();
        }
    }
    @Override
    public Add_Contacts_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        itemView = LayoutInflater.from(MyApplication.getContext())
                .inflate(R.layout.contacts_item_layout,parent,false);

        Add_Contacts_Adapter.ViewHolder viewHolder = new Add_Contacts_Adapter.ViewHolder(itemView);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        setCount();
        final Contacts contact=list.get(position);
        holder.name.setText(contact._name);
        holder.address.setText(contact.address);
        holder.phonenum.setText(contact._tel);
        holder.namep.setText(contact._name);
        RandomColor randomColor=new RandomColor();
        int color=randomColor.randomColor();
        holder.namep.setFillColor(color);

        if(hashMap.get(contact._tel)!=null){
            holder.checkBox.setChecked(!holder.checkBox.isChecked());
        }

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view){
                    if( !contact.isEnabled ){
                        holder.checkBox.setChecked(contact.isEnabled);
                        Toast.makeText(MyApplication.getContext(), "请选择手机号码", Toast.LENGTH_SHORT).show();
                    }else if (holder.checkBox.isChecked()) {
                        holder.checkBox.setChecked(!holder.checkBox.isChecked());
                    } else if(!holder.checkBox.isChecked()&&hashMap.size()<3){
                        holder.checkBox.setChecked(!holder.checkBox.isChecked());
                    } else if (hashMap.size()==3){
                        Toast.makeText(MyApplication.getContext(), "最多只能选择3人", Toast.LENGTH_SHORT).show();
                    }
            }
        });
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if( !contact.isEnabled ){
                    holder.checkBox.setChecked(contact.isEnabled);
                    Toast.makeText(MyApplication.getContext(), "请选择手机号码", Toast.LENGTH_SHORT).show();
                }else if (hashMap.size()==3&&b){
                    Toast.makeText(MyApplication.getContext(), "最多只能选择3人", Toast.LENGTH_SHORT).show();
                    holder.checkBox.setChecked(!holder.checkBox.isChecked());
                }else if(b){
                    hashMap.put(contact._tel, contact);
                    setCount();
                }else {
                    hashMap.remove(contact._tel);
                    setCount();
                }
            }
        });
    }

    public void setCount(){
        Add_Contacts_Activity.getTextViewContactsCount(hashMap.size()+"");
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
